package com.yingxin.yx.framework.user.exception;

import com.yingxin.yx.framework.commons.GlobalCode;
import com.yingxin.yx.framework.commons.GlobalInstance;
import com.yingxin.yx.framework.commons.StatusCode;
import com.yingxin.yx.framework.commons.exception.BizException;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * @author fanchao
 */

@Data
public class UserAuthException extends BizException {


    private static final long serialVersionUID = -976926170430257201L;

    /**
     * 错误码
     */
    private String code;
    /**
     * 错误消息
     */
    private String message;

    public UserAuthException() {
        StatusCode statusCode = GlobalInstance.getInstance().getStatusCodeMap().get(GlobalCode.NO_AUTHC);
        this.code = GlobalCode.NO_AUTHC;
        this.message = statusCode.getMessage();
    }

    public UserAuthException(String code) {
        StatusCode statusCode = GlobalInstance.getInstance().getStatusCodeMap().get(code);
        if (statusCode == null) {
            statusCode = GlobalInstance.getInstance().getStatusCodeMap().get(GlobalCode.NO_AUTHC);
        }
        this.code = code;
        this.message = statusCode.getMessage();
    }

    public UserAuthException(String code, String extMessage) {
        StatusCode statusCode = GlobalInstance.getInstance().getStatusCodeMap().get(code);
        if (statusCode == null) {
            statusCode = GlobalInstance.getInstance().getStatusCodeMap().get(GlobalCode.NO_AUTHC);
        }
        this.code = code;
        this.message = StringUtils.isEmpty(extMessage) ? statusCode.getMessage() : statusCode.getMessage() + ":" + extMessage;
    }

    @Override
    public String getMessage() {
        return message;
    }

}
